package socket;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.net.NetSocket;
import io.vertx.core.parsetools.RecordParser;
public class MyVerticle2 extends AbstractVerticle {

	String ip = "10.0.164.24";
	int port = 3001;

	@Override
	public void start() throws Exception {

		EventBus bus = vertx.eventBus();
		bus.publish("com.test", "test");
		bus.publish("com.test", "test2");
		bus.publish("com.test", "test3");

		System.out.println("2:" + vertx);
		vertx.createNetClient().connect(port, ip, asyncResult -> {
			if(asyncResult.succeeded()){
				NetSocket netSocket = asyncResult.result();
				netSocket.handler( buffer ->{

					//TCP粘包拆包处理
					RecordParser parser = RecordParser.newDelimited("\r\n", h -> {
						String s = String.format("设备返回： %s", h.toString());
						System.out.println(s);
					});

					parser.handle(Buffer.buffer(buffer.toString()));

//					System.out.println("client received data :"+buffer.toString());
				}).exceptionHandler(Throwable::printStackTrace);
//				netSocket.write("client send to server data");
//				netSocket.write("SU\r\n");
				netSocket.write("SU\r\n");
				netSocket.write("SU\n");

			}

		});
	}
}
